package com.macro.mall.portal.util;


import com.alibaba.fastjson.JSONObject;
import com.macro.mall.model.UmsMemberReceiveAddress;
import com.macro.mall.portal.config.ApiConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName AddressParse
 * @Description 描述
 * @Author bai
 * @Date 2024/10/22 15:51
 */

@Component
@Slf4j
public class AddressParse {


    @Autowired
    private ApiConfig apiConfig;


    public UmsMemberReceiveAddress parse(String addressInfo){

        if (!StringUtils.isNotBlank(addressInfo)){
            throw new RuntimeException("地址不能为空");
        }


//        String host = "https://kzaddress2.market.alicloudapi.com";
//        String path = "/api/address/parse";
//        String method = "POST";
//        String appcode = "437f418594b945c48d362075b52e846f";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + apiConfig.getAppCode());
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("address", addressInfo);


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse httpResponse = HttpUtils.doPost(apiConfig.getHost(),
                    apiConfig.getPath(),
                    apiConfig.getMethod(),
                    headers,
                    querys,
                    bodys);
            //获取response的body
            String data = EntityUtils.toString(httpResponse.getEntity());
            log.debug("API地址解析结果：{}",data);

            //JSON解析
            JSONObject object = JSONObject.parseObject(data);
            //判断状态码是否为200
            if (object.getInteger("code")==200){
                JSONObject resultData = object.getJSONObject("data");
                if (resultData!=null){
                    UmsMemberReceiveAddress info = new UmsMemberReceiveAddress();
                    info.setCity(resultData.getString("city"));
                    info.setName(resultData.getString("name"));
                    info.setPhoneNumber(resultData.getString("mobile"));
                    info.setProvince(resultData.getString("province"));
                    info.setPostCode(resultData.getString("zipCode"));
                    info.setDetailAddress(resultData.getString("detail"));
                    return info;
                }
            }
        } catch (Exception e) {
            log.error("API地址解析失败：{}",e.getMessage());
        }
        return null;
    }
}
